<?php

namespace Main\Api\App;

class data extends common
{
	// 保存分页
	public $page = null;


	// 解析参数
	public function query($args)
	{
		if (empty($args)) {
			return null;
		}
		parse_str($args, $query);
		foreach ($query as $k => $v) {
			$arr = explode('get_', $k);
			$v = $v == 'null' ? null : $v;
			if (count($arr) > 1) {
				$query[$arr[1]] = \FC\get($arr[1]) ?? $v;
				unset($query[$k]);
			}
			$arr = explode('post_', $k);
			if (count($arr) > 1) {
				$query[$arr[1]] = \FC\post($arr[1]) ?? $v;
				unset($query[$k]);
			}
		}
		return $query;
	}

	// 分页
	public function page($query, $_page)
	{
		if (!isset($query['url_mode'])) {
			$query['url_mode'] = 1;
		}
		if (!isset($query['url'])) {
			list($scheme, $host, $path) = array_values(parse_url(urldecode(NOW_URL)));
			$url = "{$path}?page=%u";
		} else {
			$url = \FC\url($query['url'], $query['url_mode']);
		}
		if ($_page) {
			list($page, $number, $total, $offset) = array_values($_page);
		}
		if (empty($page)) {
			return null;
		}
		$pages = [];
		$pages['list'] = [];
		if ($page > 1) {
			$pages['last'] = sprintf($url, $page - 1);
		}
		if ($page < $total) {
			$pages['next'] = sprintf($url, $page + 1);
		}
		for ($i = 1; $i <= $total; $i++) {
			$pages['list'][$i] = sprintf($url, $i);
		}
		return $pages ?? null;
	}

	// 获取文章列表
	public function getArticleList($data = [])
	{
		$data['limit'] = isset($data['page_size']) ? $data['page_size'] : $this->page_size;
		$data['page'] = $data['page'] ?? 1;
		$data['nian'] = $data['nian'] ?? null;
		$data['order'] = 'desc';
		$data['sname'] = $data['title'] ?? null;
		if ($data['sname']) {
			$data['skey'] = 'title';
		}
		$res = $this->articles_model->query($data);
		$datas = [];
		if ($res['data']) {
			$datas = $res['data'];
			$this->page['article'] = $res['page'];
			array_walk($datas, function (&$v, $k) {
				$v['remark'] = \Tools\substrText($v['content'], 0, 40);
				$v['cdat'] = date("Y-m-d H:i:s", $v['cdat']);
				if(empty($v['cover'])){
					$v['cover'] = 'default.jpg';
				}
				$v['cover'] = $this->cover_url . $v['cover'];
			});
		}
		return $datas;
	}

	// 获取文章详情
	public function getArticleInfo(int $id)
	{
		if (empty($id)) {
			return null;
		}
		$res = $this->articles_model->get($id);
		if ($res) {
			array_walk($res, function (&$v, $k) {
				switch ($k) {
					case "cdat":
						$v = date("Y-m-d H:i:s", $v);
						break;
				}
			});
		}
		return $res;
	}

	// 获取一条随机文章
	public function getRandArticle()
	{
		$res = $this->articles_model->rand_views();
		if ($res) {
			array_walk($res, function (&$v, $k) {
				switch ($k) {
					case "cdat":
						$v = date("Y-m-d H:i:s", $v);
						break;
				}
			});
		}
		return $res;
	}

	// 获取分类列表
	public function getClassList($data = [])
	{
		$data['limit'] = isset($data['page_size']) ? $data['page_size'] : $this->page_size;
		$data['page'] = $data['page'] ?? 1;
		$data['order'] = isset($data['order']) ? $data['order'] : 'asc';
		$res = $this->categorys_model->query($data);
		$datas = [];
		if ($res['data']) {
			$datas = $res['data'];
			array_walk($datas, function (&$v, $k) {
				$v['cdat'] = date("Y-m-d H:i:s", $v['cdat']);
			});
		}
		return $datas;
	}

	// 获取分类标题
	public function getClassTitle(int $cid)
	{
		if (empty($cid)) {
			return null;
		}
		$data['cid'] = $cid;
		$res = $this->categorys_model->getOnly($data, 'title');
		return $res['title'] ?? null;
	}

	// 获取微语列表
	public function getWeiyuList($data = [])
	{
		$data['limit'] = isset($data['page_size']) ? $data['page_size'] : $this->page_size;
		$data['page'] = $data['page'] ?? 1;
		$data['order'] = 'desc';
		$data['sname'] = $data['title'] ?? null;
		if ($data['sname']) {
			$data['skey'] = 'title';
		}
		$res = $this->weiyu_model->query($data);
		$datas = [];
		if ($res['data']) {
			$datas = $res['data'];
			$this->page['weiyu'] = $res['page'];
			array_walk($datas, function (&$v, $k) {
				$v['cdat'] = date("Y-m-d H:i:s", $v['cdat']);
			});
		}
		return $datas;
	}

	// 获取微语详情
	public function getWeiyuInfo(int $id)
	{
		if (empty($id)) {
			return null;
		}
		$res = $this->weiyu_model->get($id);
		return $res;
	}

	// 获取友链列表
	public function getLinksList($data = [])
	{
		$data['limit'] = isset($data['page_size']) ? $data['page_size'] : $this->page_size;
		$data['page'] = $data['page'] ?? 1;
		$data['order'] =  $data['order'] ?? 'asc';
		$data['sname'] = $data['title'] ?? null;
		if ($data['sname']) {
			$data['skey'] = 'title';
		}
		$res = $this->links_model->query($data);
		$datas = [];
		if ($res['data']) {
			$datas = $res['data'];
			$this->page['links'] = $res['page'];
			//array_walk($datas, function (&$v, $k) {
			//$v['remark'] = empty($v['remark']) ? \Tools\substrText($v['content'], 0, 150) :  $v['remark'];
			//});
		}
		return $datas;
	}

	// 获取友链详情
	public function getLinksInfo(int $id)
	{
		if (empty($id)) {
			return null;
		}
		$res = $this->links_model->get($id);
		return $res;
	}

	// 获取评论列表
	public function getCommentsList($data = [])
	{
		$data['limit'] = isset($data['page_size']) ? $data['page_size'] : $this->page_size;
		$data['page'] = $data['page'] ?? 1;
		$data['order'] =  $data['order'] ?? 'desc';
		//print_r($data);
		//die();
		$res = $this->comments_model->query($data);
		$datas = [];
		if ($res['data']) {
			$datas = $res['data'];
			$this->page['comments'] = $res['page'];
			array_walk($datas, function (&$v, $k) {
				//echo $v['cdat'].PHP_EOL;
				$v['cdat'] = date("Y-m-d H:i:s", $v['cdat']);
			});
		}
		return $datas;
	}
		
}
